package com.asia5b.a5mall.middleware.logic.viewmodel.home

import androidx.lifecycle.MutableLiveData
import com.asia5b.a5mall.middleware.base.SimpleNetParams
import com.asia5b.a5mall.middleware.base.SimpleViewModel
import com.asia5b.a5mall.middleware.model.bean.HomeMultiItemEntity
import com.asia5b.a5mall.middleware.model.home.*
import com.asia5b.a5mall.middleware.model.shop.AgreementModel
import com.asia5b.a5mall.middleware.utils.JsonUtil
import com.asia5b.a5mall.middleware.utils.Preference
import io.reactivex.Observable
import io.reactivex.functions.Consumer

/**
 * Created by zhangyuncai on 2020/8/19.
 */
class HomeFragmentVM : SimpleViewModel() {


    private var floorText: String by Preference<String>("floorText", "")
    private var bannerText: String by Preference<String>("bannerText", "")
    private var menuText: String by Preference<String>("menuText", "")

    val loadFloorLiveData = MutableLiveData<MutableList<HomeMultiItemEntity>>()

    /**
     * 加载首页数据
     */
    fun loadFloor(refresh: Int) {
        if (true) {
            floorText = ""
            bannerText = ""
            menuText = ""
        }
        val netParams = SimpleNetParams(isShowLoading = (refresh != 1))
        if (refresh != 0) {
            floorText = ""
            bannerText = ""
            menuText = ""
        } else {
            if (floorText.isNotEmpty() || bannerText.isNotEmpty() || menuText.isNotEmpty()) {

                //todo 加载缓存
                var result = ArrayList<Any>()
                val floorList = JsonUtil.jsonToArray(floorText, HomeFloorItemModel::class.java)
                        ?: arrayListOf<HomeFloorItemModel>()
                val menuList = JsonUtil.jsonToArray(floorText, HomeMenuModel::class.java)
                        ?: arrayListOf<HomeMenuModel>()
                val bannerList = JsonUtil.jsonToArray(floorText, HomeBannerMoel::class.java)
                        ?: arrayListOf<HomeBannerMoel>()
                //todo 新增banner图
                result.add(bannerList)
                //todo 新增 菜单图
                result.add(menuList)
                //todo 新增底部各式各样的商品
                result.add(floorList)
                renderFloor(result)
//                providerView().renderFloor(result)
            }
        }
        //todo 底部各式各样的商品
        val floor = extraApi.getFloor().map { it.getHomeFloorModelList() }
        //todo banner图
        val banner = extraApi.getBanner()
        //todo 菜单
        val menu = extraApi.getMenu()
        val zip = Observable.zip(floor, menu, banner,
                io.reactivex.functions.Function3<ArrayList<HomeFloorItemModel>,
                        ArrayList<HomeMenuModel>, ArrayList<HomeBannerMoel>,
                        ArrayList<Any>> { floorList, menuList, bannerList ->

                    var result = ArrayList<Any>()

                    //todo 新增banner图
                    result.add(bannerList)
                    //todo 新增 菜单图
                    result.add(menuList)
                    //todo 新增底部各式各样的商品
                    result.add(floorList)

                    //todo 缓存起来
                    floorText = JsonUtil.anyToJson(floorList)
                    menuText = JsonUtil.anyToJson(menuList)
                    bannerText = JsonUtil.anyToJson(bannerList)


                    return@Function3 result

                })

        baseSubscribe(zip, netParams, Consumer { result ->
            renderFloor(result)
//            providerView().renderFloor(result)
        }, Consumer {
            floorText = ""
            menuText = ""
            bannerText = ""
        })
        getSeckillTimeLine()
    }

    private fun renderFloor(result: ArrayList<Any>) {
        val list= mutableListOf<HomeMultiItemEntity>()

        //todo 展示banner
        val bannerList = result[0] as ArrayList<HomeBannerMoel>
        if (bannerList.size > 0) {
            list.add(HomeBannerContainerBean(bannerList))
        }
        //todo 展示菜单
        val menuList = result[1] as ArrayList<HomeMenuModel>
        if (menuList.size > 0) {
            list.add(HomeMenuContainerBean(menuList))
        }
        //展示其他
        for (datum in (result[2] as ArrayList<HomeFloorItemModel>)) {
            if (datum is HomeMultiItemEntity) {
                when (datum.itemType) {
                    HomeMultiItemEntity.FloorSingleImage,
                    HomeMultiItemEntity.FloorRightOneLeftTwo,
                    HomeMultiItemEntity.FloorThreeImage,
                    HomeMultiItemEntity.FloorFiveImage,
                    HomeMultiItemEntity.FloorCornerBanner,
                    HomeMultiItemEntity.FloorFourImage,
                    HomeMultiItemEntity.FloorFourImage_31,
                    HomeMultiItemEntity.FloorTitle,
                    HomeMultiItemEntity.FloorLeftRight,
                    HomeMultiItemEntity.FloorLeftRight,
                    HomeMultiItemEntity.FloorText,
                    HomeMultiItemEntity.MemberRecommendGoods,
                    HomeMultiItemEntity.FloorLeftOneRightTwo
                    -> {
                        list.add(datum)
                    }
                }

            }
        }
        loadFloorLiveData.value=list
    }

    fun getSeckillTimeLine() {
        baseSubscribe(promotionApi.getSeckillTimeLine(), SimpleNetParams(isShowLoading = false), Consumer {

        })
    }


    fun getApplyShopAgreeContent(callback: Consumer<AgreementModel>) {
        val observable = shopApi.getApplyShopState()
                .flatMap {
                    if (it.isCanApplyShop()) {
                        return@flatMap baseApi.getApplyShopAgreeContent()
                    } else {
                        return@flatMap Observable.just(AgreementModel())
                    }
                }
        baseSubscribe(observable, callback)
    }
}